/*************************************************************************
 *
 *  NSIS Setup script
 *
 * Requires: Unicode NSIS 2.46 http://www.scratchpaper.com/
 *
 * MIT License
 *
 * Copyright (c) The individual, corporate, and institutional contributors
 * who have collectively contributed elements to this software 
 * ("The Squeak Community"), 1996-2011 All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 *
 *************************************************************************/

SetCompressor /SOLID lzma

###################################
# Includes
###################################
!include "MUI.nsh"
!include "FileFunc.nsh"
!include "FileAssoc.nsh"
!include "FileAssociation.nsh"

###################################
# Definitions
###################################
!define /date TIMESTAMP "%Y-%m-%d-%H-%M-%S"
!define /date PRODUCT_DATE "%y%m%d"							# The release date of the setup
!define REG_KEY  "SOFTWARE\${PRODUCT_NAME}"
!define TARGET_DIR "$DOCUMENTS\${PRODUCT_NAME} ${PRODUCT_VERSION}"
!define APPLICATION_DIR "Application"
!define RESOURCE_DIR "Resources"
!define /date YEAR "%Y"

!define PRODUCT_UNINSTALL_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"

# Import additional definitions
!include "${APPLICATION_DIR}\Definitions.nsh"				# Imported from Squeak Smalltalk

###################################
# Modern User Interface settings
###################################
!define MUI_ICON   "${RESOURCE_DIR}\${PRODUCT_NAME}.ico"

!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "${RESOURCE_DIR}\header.bmp"
!define MUI_WELCOMEFINISHPAGE_BITMAP "${RESOURCE_DIR}\welcome.bmp"
!define MUI_FINISHPAGE_NOAUTOCLOSE							# Do not automatically jump to the finish page, to allow the user to check the install log.
!define MUI_UNFINISHPAGE_NOAUTOCLOSE						# Do not automatically jump to the finish page, to allow the user to check the uninstall log.
!define MUI_FINISHPAGE_RUN "$INSTDIR\${CONSTANT_VM_FILE}"			# Allows to run the application after install

###################################
# Pages
###################################

# Installer pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "${RESOURCE_DIR}\license.rtf"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

# Uninstaller pages
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

###################################
# Language
###################################
!insertmacro MUI_LANGUAGE "English"

###################################
# Compiler commands
###################################
Name         "${PRODUCT_NAME}"
OutFile      "${PRODUCT_NAME}-${PRODUCT_VERSION}-${CONSTANT_IMAGE_VERSION}-${PRODUCT_DATE}-installer.exe"
InstallDir   "${TARGET_DIR}"								# Default installation directory
InstallDirRegKey HKCU "${REG_KEY}" ""						# Get installation from registry if available
RequestExecutionLevel Admin									# Request Administrator privileges
BrandingText "${PRODUCT_NAME} Version ${PRODUCT_VERSION}"
XPStyle      on
CRCCheck     on
SetOverwrite on
SetCompress  force

###################################
# Version Tab in File Properties
###################################

VIProductVersion "${PRODUCT_VERSION}.0.0"
VIAddVersionKey ProductName     "${PRODUCT_NAME}"
VIAddVersionKey ProductVersion  "${PRODUCT_VERSION}"
VIAddVersionKey CompanyName     "${PRODUCT_PUBLISHER}"
VIAddVersionKey CompanyWebsite  "${PRODUCT_WEBSITE}"
VIAddVersionKey FileVersion     ""
VIAddVersionKey FileDescription ""
VIAddVersionKey LegalCopyright  "(C) ${YEAR} ${PRODUCT_PUBLISHER}"

Function .onInit
   InitPluginsDir
   Push $R1
   File /oname=$PLUGINSDIR\spltmp.bmp ${RESOURCE_DIR}\InstallerSplash.bmp
   advsplash::show 3000 600 400 -1 $PLUGINSDIR\spltmp
   Pop $R1
   Pop $R1
FunctionEnd

Function un.onUninstSuccess
   HideWindow
   MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) has been uninstalled."
FunctionEnd

# Main Section
Section 
	SetOutPath "$INSTDIR"
	DetailPrint "Installation Started."
	
	UserInfo::GetAccountType
	pop $0
	${If} $0 != "admin" # Require admin rights on NT4+
		MessageBox mb_iconstop "Administrator rights required!"
		SetErrorLevel 740 # ERROR_ELEVATION_REQUIRED
		Quit
	${EndIf}
	
	WriteUninstaller "$INSTDIR\Uninstall.exe"
	
	DetailPrint "Writing Registry Entries."
	WriteRegStr HKCU "${REG_KEY}" "" $INSTDIR
	
	# Add to Remove Software Control Panel
	WriteRegStr HKLM "${PRODUCT_UNINSTALL_KEY}" "DisplayName" "${PRODUCT_NAME}"
	WriteRegStr HKLM "${PRODUCT_UNINSTALL_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
	WriteRegStr HKLM "${PRODUCT_UNINSTALL_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
	WriteRegStr HKLM "${PRODUCT_UNINSTALL_KEY}" "UninstallString" "$\"$INSTDIR\Uninstall.exe$\""
	WriteRegStr HKLM "${PRODUCT_UNINSTALL_KEY}" "QuietUninstallString" "$\"$INSTDIR\Uninstall.exe$\" /S"
	
	DetailPrint "Create Program Group."
	CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME} ${PRODUCT_VERSION}"
	CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME} ${PRODUCT_VERSION}\${PRODUCT_NAME}.lnk" "$INSTDIR\${CONSTANT_VM_FILE}"
	CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME} ${PRODUCT_VERSION}\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
SectionEnd

Section "${PRODUCT_NAME} VM ${CONSTANT_VM_VERSION}" Section1
	SetOutPath "$INSTDIR"
	DetailPrint "Installing ${PRODUCT_NAME} VM."
	
	File "${RESOURCE_DIR}\license.rtf"
	File "${APPLICATION_DIR}\${CONSTANT_VM_FILE}"
	File "${APPLICATION_DIR}\${CONSTANT_INI_FILE}"
	File "${APPLICATION_DIR}\splash.bmp"
	File "${APPLICATION_DIR}\SqueakFFIPrims.dll"
	File "${APPLICATION_DIR}\FT2Plugin.dll"
SectionEnd

Section "Image and Sources" Section2
	SetOutPath "$INSTDIR"
	DetailPrint "Installing Image and Sources files."
	
	File "${APPLICATION_DIR}\${CONSTANT_CHANGES_FILE}"
	File "${APPLICATION_DIR}\${CONSTANT_IMAGE_FILE}"
	File "${APPLICATION_DIR}\${CONSTANT_SOURCE_FILE}"
SectionEnd

Section "Desktop shortcut" Section3
	SetOutPath "$INSTDIR"
	DetailPrint "Creating Desktop Shortcut."
	
	CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\${CONSTANT_VM_FILE}"
SectionEnd

Section "File association" Section4
	SetOutPath "$INSTDIR"
	DetailPrint "Creating File Association."
	
	${RegisterExtension} "$INSTDIR\${CONSTANT_VM_FILE}" ".image" "${PRODUCT_NAME} Image File"
	!insertmacro UPDATEFILEASSOC
SectionEnd

Section "Uninstall"
   SetOutPath $INSTDIR
   DetailPrint "Start uninstalling"
   
   DetailPrint "Deleting ${PRODUCT_NAME} files."
   Delete "$INSTDIR\license.rtf"
   Delete "$INSTDIR\${CONSTANT_VM_FILE}"
   Delete "$INSTDIR\${CONSTANT_INI_FILE}"
   Delete "$INSTDIR\splash.bmp"
   Delete "$INSTDIR\SqueakFFIPrims.dll"
   Delete "$INSTDIR\FT2Plugin.dll"
   Delete "$INSTDIR\${CONSTANT_CHANGES_FILE}"
   Delete "$INSTDIR\${CONSTANT_IMAGE_FILE}"
   Delete "$INSTDIR\${CONSTANT_SOURCE_FILE}"
   Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
   
   # Remove from Remove Software Control Panel
   DetailPrint "Remove Registry Entries."
   DeleteRegKey HKLM "${PRODUCT_UNINSTALL_KEY}"
   DeleteRegKey /ifempty HKCU "${REG_KEY}"
   
   # Remove file association
   DetailPrint "Remove File Association."
   ${UnRegisterExtension} ".image" "${PRODUCT_NAME} Image File"
   !insertmacro UPDATEFILEASSOC
   
   Delete "$INSTDIR\Uninstall.exe"
   RmDir /r /REBOOTOK "$SMPROGRAMS\${PRODUCT_NAME} ${PRODUCT_VERSION}"
   RmDir /REBOOTOK $INSTDIR
   
   SetAutoClose true
SectionEnd

###################################
# Section description
###################################
LangString DESC_Section1 ${LANG_ENGLISH} "The ${PRODUCT_NAME} virtual machine and components."
LangString DESC_Section2 ${LANG_ENGLISH} "The ${PRODUCT_NAME} image and source files."
LangString DESC_Section3 ${LANG_ENGLISH} "Create a ${PRODUCT_NAME} desktop shortcut."
LangString DESC_Section4 ${LANG_ENGLISH} "Associate Image files with ${PRODUCT_NAME}."
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
   !insertmacro MUI_DESCRIPTION_TEXT ${Section1} $(DESC_Section1)
   !insertmacro MUI_DESCRIPTION_TEXT ${Section2} $(DESC_Section2)
   !insertmacro MUI_DESCRIPTION_TEXT ${Section3} $(DESC_Section3)
   !insertmacro MUI_DESCRIPTION_TEXT ${Section4} $(DESC_Section4)
!insertmacro MUI_FUNCTION_DESCRIPTION_END